package com.ybl.store.backend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybl.store.backend.dao.entity.StoreDO;
import com.ybl.store.backend.dao.mapper.StoreMapper;
import com.ybl.store.backend.dto.req.StoreSaveReqDTO;
import com.ybl.store.backend.dto.req.StoreUpdateReqDTO;
import com.ybl.store.backend.dto.resp.ListRespDTO;
import com.ybl.store.backend.dto.resp.StoreNameRespDTO;
import com.ybl.store.backend.dto.resp.StorePageRespDTO;
import com.ybl.store.backend.service.StoreService;
import com.ybl.store.common.constant.DelFlag;
import com.ybl.store.common.convention.errorcode.BaseErrorCode;
import com.ybl.store.common.convention.exception.ClientException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, StoreDO> implements StoreService {

    @Override
    public void saveStore(StoreSaveReqDTO requestParam) {
        int result = baseMapper.insert(BeanUtil.toBean(requestParam, StoreDO.class));

        if (result < 1) {
            throw new ClientException(BaseErrorCode.STORE_SAVE_ERROR);
        }
    }

    @Override
    public void deleteStoreById(Long id) {
        int result = baseMapper.deleteById(id);

        if (result < 1) {
            throw new ClientException(BaseErrorCode.STORE_DELETE_ERROR);
        }
    }

    @Override
    public void updateStore(StoreUpdateReqDTO requestParam) {
        int result = baseMapper.updateById(BeanUtil.toBean(requestParam, StoreDO.class));

        if (result < 1) {
            throw new ClientException(BaseErrorCode.STORE_UPDATE_ERROR);
        }
    }

    @Override
    public StorePageRespDTO pageStore(Integer pn, Integer ps) {
        Page<StoreDO> page = new Page<>(pn, ps);
        LambdaQueryWrapper<StoreDO> lambdaQueryWrapper = Wrappers.lambdaQuery(StoreDO.class)
                .eq(StoreDO::getDelFlag, DelFlag.present);
        Page<StoreDO> storeDOPage = baseMapper.selectPage(page, lambdaQueryWrapper);

        return StorePageRespDTO
                .builder()
                .total(storeDOPage.getTotal())
                .items(storeDOPage.getRecords())
                .build();
    }

    @Override
    public ListRespDTO<StoreNameRespDTO> listStoreName() {
        LambdaQueryWrapper<StoreDO> lambdaQueryWrapper = Wrappers.lambdaQuery(StoreDO.class)
                .eq(StoreDO::getDelFlag, DelFlag.present);;
        List<StoreDO> storeDOList = baseMapper.selectList(lambdaQueryWrapper);

        return ListRespDTO
                .<StoreNameRespDTO>builder()
                .total((long) storeDOList.size())
                .items(storeDOList
                        .stream()
                        .map(each -> BeanUtil.toBean(each, StoreNameRespDTO.class))
                        .toList())
                .build();
    }
}
